﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace JetnettAutoUpdater
{
    static class Program
    {
        public const string OnlineManifestsDirectory = "http://developer.jetnett.com/Manifests/";
        public const string BaseURL = "http://developer.jetnett.com/";
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            FormMain main = new FormMain();
            Application.Run();
        }

        public static string[] GetManifestFilesFromServer()
        {
            return GetFilesFromServerDirectory(OnlineManifestsDirectory);
        }
        public static string[] GetFilesFromServerDirectory(string url)
        {
            var files = new List<string>();
            var doc = XDocument.Load(url);

            if (doc.Root == null)
                return null;

            foreach (var el in doc.Root.Elements().Where(el => el.Name == "file"))
            {
                foreach (var element in el.Elements())
                {
                    if (element.Name == "name")
                        files.Add(element.Value);
                }
            }

            return files.ToArray();
        }
        public static AppConfigSettings GetSettingsForApp(string manifestFileURL)
        { 
            var currentApp = new AppConfigSettings();
            XmlDocument doc = null;

            doc = new XmlDocument();
            doc.Load(Program.OnlineManifestsDirectory + manifestFileURL);

            var node = doc.SelectSingleNode("//appSettings");

            if (node == null)
                return null;
            foreach (XmlNode n in node.ChildNodes)
            {
                if (n.Attributes == null) continue;
                var key = n.Attributes["key"].Value;
                var value = n.Attributes["value"].Value;

                switch (key)
                {
                    case "LocalDirectory":
                        currentApp.LocalDirectory = value;
                        break;
                    case "ExecuteFile":
                        currentApp.ExecuteFile = value;
                        break;
                    case "RemoteURL":
                        currentApp.RemoteURL = value;
                        break;
                    case "Name":
                        currentApp.Name = value;
                        break;
                    case "IconPath":
                        currentApp.IconPath = value;
                        break;
                    case "StartMenuFolder":
                        currentApp.StartMenuFolder = value;
                        break;
                    case "Version":
                        currentApp.Version = value;
                        break;
                }
            }
            return currentApp;
        }
    }
}
